package com.yunhuajinfu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yunhuajinfu.R;
import com.yunhuajinfu.base.BaseActivity;
import com.yunhuajinfu.http.HttpCallBack;
import com.yunhuajinfu.http.HttpUtils;
import com.yunhuajinfu.mian.Config;
import com.yunhuajinfu.tool.StatusBarUtil;
import com.yunhuajinfu.tool.VibratorUtil;
import com.yunhuajinfu.util.DES;
import com.yunhuajinfu.view.ClearEditText;
import com.yunhuajinfu.view.TitleBar;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * yihuangxing  注册
 */
public class RegisterActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.et_password)
    ClearEditText etPassword;
    @Bind(R.id.et_password_password)
    ClearEditText etPasswordPassword;
    @Bind(R.id.et_yaoqingma)
    ClearEditText etYaoqingma;
    private String userAccount;
    private String phoneCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        StatusBarUtil.setTranslucent(this, 0);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        titlebar.setTitleBar(R.drawable.back_selector, "注册", 0);
        titlebar.setTitleBarBackgroundColor(getResources().getColor(R.color.tou_ming));
        //
        userAccount = getIntent().getStringExtra("userAccount");
        phoneCode = getIntent().getStringExtra("phoneCode");
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {

    }

    @OnClick({R.id.titlebar_iv_back, R.id.tv_register})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.titlebar_iv_back:


                finish();

                break;

            case R.id.tv_register:
                long[] pattern = {100, 400, 100, 400};
                String password = etPassword.getText().toString().trim();
                String paspaswrod = etPasswordPassword.getText().toString().trim();
                String yaoqingma = etYaoqingma.getText().toString().trim();

                if (TextUtils.isEmpty(password)) {
                    etPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(RegisterActivity.this, pattern, false);
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;

                } else if (etPassword.length() < 6) {
                    etPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(RegisterActivity.this, pattern, false);
                    Toast.makeText(RegisterActivity.this, "请输入6-16位密码", Toast.LENGTH_SHORT).show();
                    return;

                } else if (TextUtils.isEmpty(paspaswrod)) {
                    etPasswordPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(RegisterActivity.this, pattern, false);
                    Toast.makeText(RegisterActivity.this, "请再次输入输入确定密码", Toast.LENGTH_SHORT).show();
                    return;

                } else if (etPasswordPassword.length() < 6) {
                    etPasswordPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(RegisterActivity.this, pattern, false);
                    Toast.makeText(RegisterActivity.this, "请输入6-16位确定密码", Toast.LENGTH_SHORT).show();
                    return;

                } else if (!password.equals(paspaswrod)) {
                    etPasswordPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(RegisterActivity.this, pattern, false);
                    Toast.makeText(RegisterActivity.this, "密码不匹配,请重新输入", Toast.LENGTH_SHORT).show();

                    return;
                } else {
                    try {
                        HttpParams params = new HttpParams();
                        params.put("userPassword", DES.encryptDES(etPassword.getText().toString().trim(), Config.DES_KEY));
                        params.put("userPassword2", DES.encryptDES(etPasswordPassword.getText().toString().trim(), Config.DES_KEY));
                        params.put("inviteUserId", etYaoqingma.getText().toString().trim());
                        HttpUtils.post(Config.REGIST_THREE, params, new HttpCallBack(RegisterActivity.this, "注册中...") {
                            @Override
                            public void onSucceed(JSONObject result, String code, String message) {

                                Toast.makeText(RegisterActivity.this, message, Toast.LENGTH_SHORT).show();

                                //注册成功   回到登录页面
                                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                                finish();
                            }

                            @Override
                            public void onError(String code, String message) {

                                Toast.makeText(RegisterActivity.this, message, Toast.LENGTH_SHORT).show();
                            }
                        });


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                break;

            default:
        }

    }
}
